# coding: UTF-8 
# Filename : chain_activity_api.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-

from api.common.common import *
from api.common import mysql,log,utils,httputils,const
import chain_activity_data_model

#查询公共sql
common_sql = '''
select tb1.name, tb1.category_id, tb1.start_time, tb1.end_time, 
tb1.store_in_app, tb1.description, tb1.id, tb1.images, tb2.class_name, 
tb1.list_thumb, tb1.summary
from chain_activity tb1 left join  p_class tb2
on tb1.category_id = tb2.id
where 1 = 1
'''

#替换字符
#sbu_flag 区分是否需要过滤html字符
#ratio适配信息
#is_thumb是否是缩略图
def replace_body(obj ,sub_flag, ratio, is_thumb):
    if obj == None : return ''
    #替换模板
    body_template = chain_activity_data_model.imedia_body
    body_template = body_template.replace('{name}', str(obj[0]))
    body_template = body_template.replace('{category_id}', str(obj[1]))
    body_template = body_template.replace('{from}', str(obj[2]))
    body_template = body_template.replace('{to}', str(obj[3]))
    body_template = body_template.replace('{storeinapp}', str(obj[4]))
    if sub_flag:
        body_template = body_template.replace('{description}', utils.get_detail_text(str(obj[5])))
    else:
        body_template = body_template.replace('{description}', str(obj[5]))
    body_template = body_template.replace('{id}', str(obj[6]))
    body_template = body_template.replace('{image}', image_path(obj[7], ratio, False))
    obj8 = obj[8]
    if obj8 == None: obj8 = ''
    body_template = body_template.replace('{category_name}', str(obj8))
    body_template = body_template.replace('{thumb}', image_path(obj[9], ratio, True))
    body_template = body_template.replace('{summary}', str(obj[10]))
    return body_template
        
def get_detail_str(activity_id, ratio):
    #数据库查询
    sql = common_sql + '''
    and tb1.id = %s
    and tb1.active = 1
    '''
    obj = mysql.get(sql, [int(activity_id)])
    #替换模板
    return replace_body(obj, False, ratio, False)

#获取中间数据体
def get_body_str(cus_id, size, pagenum, ratio, storeinapp='0'):
     #封装storeinapp条件
    condition = storeinapp
    #sql
    sql = common_sql + '''
    and tb1.cus_id = %s
    and tb1.active = 1
    and store_in_app in ({condition})
    order by tb1.id desc
    limit %s, %s
    '''
    sql = sql.replace('{condition}', condition)
    #获取起始行
    start_row = utils.get_star_row(size, pagenum)
    param = [int(cus_id), start_row, int(size)]
    rsts = mysql.doSearch(sql, param)
    body_str = ''
    for obj in rsts:
        body_str += replace_body(obj, True, ratio, True)
    return body_str

#获取详细列表http://api.imedia.com/app/activity/list
def chain_activity_list(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #获取请求参数
    cus_id = httputils.post(request, 'cus_id')
    size = httputils.post(request, 'size')
    pagenum = httputils.post(request, 'pagenum')
    ratio = httputils.post(request, 'ratio')
    #获取storeinapp
    storeinapp = httputils.post(request, 'storeinapp')
    if storeinapp == None or storeinapp.strip() == '': storeinapp = '0'
    storeinapp_arr = storeinapp.split(',')
    for storeinapp_obj in storeinapp_arr:
        if not utils.is_num(storeinapp_obj): storeinapp = '0'
    
    if not utils.is_num(size): size = 5
    if not utils.is_num(pagenum): pagenum = 1
    if not utils.is_num(cus_id): cus_id = 1
    #通信验证
    if validate(request):
        return validate(request)
    
    #中间模板段
    output = ''
    output += chain_activity_data_model.imedia_start 
    output += get_body_str(cus_id, size, pagenum, ratio, storeinapp)
    output += chain_activity_data_model.imedia_end
    #初始化xml返回
    return response(output)

#获取单个信息http://api.imedia.com/app/activity
def chain_activity_detail(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    
    activity_id = httputils.post(request, 'activity_id')
    ratio = httputils.post(request, 'ratio')
    uid = httputils.post(request, 'uid')
    if uid == None or uid.strip() == '':
        error_info = chain_activity_data_model.error_model_107
        return response(error_info)
    id_array = activity_id.split(',')
    output = ''
    output += chain_activity_data_model.imedia_start
    for obj_id in id_array :
        if not utils.is_num(obj_id): obj_id = 0 #做验证防止异常发生
        output += get_detail_str(obj_id, ratio)
    output += chain_activity_data_model.imedia_end
    #初始化xml返回 
    return response(output)


